<?php
namespace backend\model;
use backend\model\admin_access;
use backend\model\admin_role;
use common\model\common_model;
/**
 * role_auth模型数据库操作类
 */

class role_auth extends common_model {
	public $table_name = 'role_auth';
	public $category = '';
	public $_pk  = 'id';
	public function __construct() {
		$this->db_config = load_config('database');
		$this->db_setting = 'default';
		parent::__construct();
	    
	}

	public function getRoleAuthData($role_id)
    {
        $access_model = new admin_access();
        $access_list = $access_model->getTreeData(0,0,true);

        $auth_list = $this->select(['role_id'=>$role_id],'*');

        $ret = [];
        foreach($access_list AS &$v){
            $ret[$v['id']] = [
                'id'=>$v['id'],
                'pId'=>$v['parent_id'],
                'name'=>$v['data'],
                'open'=>$v['parent_id'] ==0 ? true :false,
            ];
        }
        if(!$auth_list){
            return $ret;
        }
        $access_ids = array_keys($access_list);

        foreach($auth_list AS &$a){
            if(in_array($a['access_id'] , $access_ids)){
                $ret[$a['access_id']]['checked'] = true;
            }
        }

        return $ret;
    }

    /**
     * 保存角色的授权数据
     * @param $role_id
     * @param $auth_data
     * @return bool
     * @throws \Exception
     */
    public function saveRoleAuthData($role_id,$auth_data)
    {
        if(empty($role_id)){
            throw new \Exception('角色id不能为空',-1000);
        }
        if(empty($auth_data)){
            throw new \Exception('授权信息不能为空',-1020);
        }
        if(is_string($auth_data)){
            $auth_data = explode(',',$auth_data);
        }
        $this->delete(['role_id'=>$role_id]);
        foreach($auth_data AS $a){
            $_data = [
                'role_id'=>$role_id,
                'access_id'=>$a,
                'add_time'=>time(),
            ];
            $ret = $this->insert($_data);
            if(!$ret){
                throw  new \Exception($this->getError(),-1030);
            }
        }
        return true;
    }

    public function getRoleAuthMenu($role_id,$path_info)
    {
        $access_model = new admin_access();
        $data_list = $access_model->getTreeData(0,0,true,false);
        if($role_id != admin_role::ROLE_ID_SUPERADMIN){
            $role_auth_list = $this->select(['role_id'=>$role_id],'access_id','','','','access_id');
            $rold_access_ids = array_keys($role_auth_list);
            //过滤不在用户访问权限内的操作
            foreach($data_list AS $key =>&$item){
                if(!in_array($item['id'],$rold_access_ids)){
                    unset($data_list[$key]);
                }
            }
        }
        
        $_list = [];
        foreach($data_list AS $key=>&$value)
        {
            if($value['level'] == 0){
                $_list[$value['id']] = $value;
                if($value['has_children']){
                    $i = 0;
                    foreach($data_list AS $_key=>&$_value) {
                        if ($_value['info']['parent_id'] == $value['id']) {
                            $i++;
                            $_list[$value['id']]['children'][$i] = $_value;

                            if ($_value['has_children']) {
                                $x = 0;
                                foreach ($data_list AS $__key => &$__value) {
                                    if ($__value['info']['parent_id'] == $_value['id']) {
                                        $x++;
                                        if (!empty($_list[$value['id']]['children'][$i])) {
                                            $_list[$value['id']]['children'][$i]['children'][$x] = $__value;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

        $html = '';
        $has_children_li = 'class="nav-item nav-item-has-subnav"';
        foreach($_list AS $k=>&$v){
        $li_class = $v['has_children'] ? $has_children_li : '';
        $html.='<li '.$li_class .'><a href="javascript:void(0)"><i class="mdi mdi-menu"></i> '.$v['data'].'</a>';
        if(isset($v['children'])){
            $html.='<ul class="nav nav-subnav">';
            foreach($v['children'] AS $_k=>&$_v){
                    $li_class2 = isset($_v['children']) ? $has_children_li : '';
                    $href_class2 = isset($_v['children']) ? '#!': $_v['info']['access_rule'];
                    $isCurrentLi2 = $this->isCurrentMenuItem($_v['info']['access_rule'],$path_info) ? 'class="active"' : '';
                    $html.='<li '.$li_class2 .$isCurrentLi2 .'>';
                    $html .= '<a href="'.$href_class2.'">'.$_v['data'].'</a>';
                    if(isset($_v['children'])) {
                        $html .= '<ul class="nav nav-subnav">';
                        foreach($_v['children'] AS $__k=>&$__v){
                                $li_class3 = isset($__v['children']) ? $has_children_li : '';
                            $isCurrentLi3 = $this->isCurrentMenuItem($__v['info']['access_rule'],$path_info) ? 'class="active"' : '';
                                $html.='<li '.$li_class3 .$isCurrentLi3.'>';
                                $html .= '<a href="'. $__v['info']['access_rule'] .'">'.$__v['data'].'</a>';
                                $html.='</li>';
                        }
                        $html.='</ul>';
                    }
                    $html.='</li>';
            }
            $html.='</ul>';
        }
        $html.='</li>';


        }
    return $html;
    }

    /**
     * 判断当前链接是否是当前指向的菜单选项
     * @param $access_rule
     * @param $path_info
     * @return bool
     */
    private function isCurrentMenuItem($access_rule,$path_info)
    {
        if(!$access_rule){
            return false;
        }
        if($access_rule === $path_info){
            return true;
        }

        return false;
    }
}
?>